package com.whut.whw.personalinfo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;

import com.whut.whw.R;
import com.whut.whw.base.BaseActivity;
import com.whut.whw.chat.ChatListFragment;
import com.whut.whw.chat.ChatWinActivity;
import com.whut.whw.opersoncenter.OPersonCenterActivity;
import com.whut.whw.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by 曾旺红 on 2017/06/22.
 */

public class FollowListActivity extends BaseActivity {

    public List<FollowListModel> list = new ArrayList<>();
    FollowListAdapter adapter;

    @BindView(R.id.rv_psndata)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @Override
    public int getLayoutId() {
        return R.layout.activity_psn_data;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initInfo();
        //指定布局方式
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        //适配器设置
        adapter = new FollowListAdapter(list);
        recyclerView.setAdapter(adapter);
        //设置toolbar
        //注：在上个页面传toolbar的字段过来的时候，使用资源文件中的
        toolbar.setTitle(R.string.follow_title);
        toolbar.setTitleTextColor(ContextCompat.getColor(context, R.color.white));
        toolbar.setNavigationIcon(R.drawable.rc_back_icon);
    }

    @Override
    public void setListener() {
        //点击事件监听
        adapter.setmOnItemClickListener(new FollowListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //getUiDelegate().toastLong(String.valueOf(position));
                LinearLayout otherInfo=(LinearLayout)view.findViewById(R.id.other_info);
                LinearLayout conversation=(LinearLayout)view.findViewById(R.id.talk);
                //点击整行，查看他人信息
                otherInfo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(FollowListActivity.this, OPersonCenterActivity.class);
                        startActivity(intent);
                    }
                });
                //点击联系，进入对话界面
                conversation.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getUiDelegate().toastLong("您点击了与ta联系");
                    }
                });
            }
        });
        //下拉刷新
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                //设置2秒的时间来执行以下事件
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        //data.add(0, "刷新后新增的item");
                        adapter.notifyDataSetChanged();
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }, 2000);
            }
        });
        //底部上滑刷新
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initInfo() {
        FollowListModel flm1 = new FollowListModel(R.drawable.psn_gz, "我是一条鱼", "从前有座山，山上有座庙", "信誉101");
        list.add(flm1);
        FollowListModel flm2 = new FollowListModel(R.drawable.psn_fs, "我是一匹狼", "横看成岭侧成峰，远近高低各不同", "信誉100");
        list.add(flm2);
        FollowListModel flm3 = new FollowListModel(R.drawable.psn_mygoods, "爱谁谁", "归去，也无风雨也无晴", "信誉102");
        list.add(flm3);
        FollowListModel flm4 = new FollowListModel(R.drawable.psn_sc, "蒙密", "孤帆远影碧空尽", "信誉101");
        list.add(flm4);
        FollowListModel flm5 = new FollowListModel(R.drawable.psn_mai, "lucky one", "你以为的你以为只是你以为", "信誉100");
        list.add(flm5);
        FollowListModel flm6 = new FollowListModel(R.drawable.psn_gz, "babybaby", "你看，那个人好像一条狗", "信誉101");
        list.add(flm6);
        FollowListModel flm7 = new FollowListModel(R.drawable.psn_tz, "木木", "假作真时真亦假，无为有时有还无", "信誉101");
        list.add(flm7);
        FollowListModel flm8 = new FollowListModel(R.drawable.psn_sc, "开开", "对酒当歌，人生几何。譬如朝露，去日苦多。慨当以慷。忧思难忘。何以解忧，唯有杜康。", "信誉101");
        list.add(flm8);
    }
}
